package com.crm.system.service.impl.customerManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.CluePoolRuleTypeEnum;
import com.crm.common.model.Enum.CustomerPoolRuleTypeEnum;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.cluePoolManager.CluePoolRule;
import com.crm.model.entity.customerManager.CustomerPoolRule;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.customerManager.CustomerPoolAddQO;
import com.crm.service.customerManager.CustomerPoolRuleDbService;
import com.crm.system.service.customerManager.CustomerPoolRuleService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/17 15:16
 */
@Service
public class CustomerPoolRuleServiceImpl implements CustomerPoolRuleService {
    @Autowired
    CustomerPoolRuleDbService customerPoolRuleDbService;

    @Override
    public Boolean addCustomerPoolRule(CustomerPoolAddQO customerPoolAddQO, String customerPoolId) {
        //配置领取规则
        insertCustomerPoolRule(customerPoolId, CustomerPoolRuleTypeEnum.RECEIVE.getCustomerPoolRuleTypeCode(),
                customerPoolAddQO.getMaximumNumber(), customerPoolAddQO.getTimeInterval(),
                customerPoolAddQO.getAnyTimeInterval(), customerPoolAddQO.getTimeLimit());

        //回退时间间隔
        insertCustomerPoolRule(customerPoolId, CustomerPoolRuleTypeEnum.RETURN.getCustomerPoolRuleTypeCode(),
                null,customerPoolAddQO.getReturnInterval(),null, null);
        return true;
    }

    public void insertCustomerPoolRule(String customerPoolId,Integer type,Integer number,Integer timeInterval,
                                       Integer anyTimeInterval, Integer timeLimit){
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerPoolRule customerPoolRule = new CustomerPoolRule();
        customerPoolRule.setCustomerPoolRuleId(CommonUtils.getGUID());
        customerPoolRule.setRuleType(type);
        customerPoolRule.setTimeInterval(timeInterval);
        customerPoolRule.setAnyTimeInterval(anyTimeInterval);
        customerPoolRule.setCustomerPoolNumber(number);
        customerPoolRule.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        customerPoolRule.setCreatePeopleId(userAccount.getAccountId());
        customerPoolRule.setCreateTm(new Date());
        customerPoolRule.setUpdatePeopleId(userAccount.getAccountId());
        customerPoolRule.setUpdateTm(new Date());
        customerPoolRule.setCustomerPoolId(customerPoolId);
        customerPoolRule.setTimeLimit(timeLimit);
        customerPoolRuleDbService.insertCustomerPoolRule(customerPoolRule);
    }

    @Override
    public Boolean deleteCustomerPoolRuleByCluePoolId(String customerPoolId) {
        return customerPoolRuleDbService.deleteCustomerPoolRuleByCluePoolId(customerPoolId);
    }

    @Override
    public JSONObject selectCustomerPoolRule(String customerPoolId) {
        JSONObject jsonObject = new JSONObject();
        List<CustomerPoolRule> customerPoolRules = customerPoolRuleDbService.selectCustomerPoolRuleByCluePoolId(customerPoolId);
        if(!customerPoolRules.isEmpty()){
            for (CustomerPoolRule customerPoolRule : customerPoolRules) {
                //分配方式：1-领取；2-回退；
                if(customerPoolRule.getRuleType() == CluePoolRuleTypeEnum.RECEIVE.getCluePoolRuleTypeCode()){
                    jsonObject.put("maximumNumber", customerPoolRule.getCustomerPoolNumber());
                    jsonObject.put("timeInterval", customerPoolRule.getTimeInterval());
                    jsonObject.put("anyTimeInterval", customerPoolRule.getAnyTimeInterval());
                    jsonObject.put("timeLimit", customerPoolRule.getTimeLimit());
                }else if(customerPoolRule.getRuleType() == CluePoolRuleTypeEnum.RETURN.getCluePoolRuleTypeCode()){
                    jsonObject.put("returnInterval", customerPoolRule.getTimeInterval());
                }
            }
        }
        return jsonObject;
    }
}
